package com.xlf.service.system.controller.common;


import com.xlf.common.core.pojo.mode.AjaxResult;
import com.xlf.common.core.pojo.mode.BaseController;
import com.xlf.common.core.util.StringUtils;
import com.xlf.common.core.util.file.FileUploadUtils;
import com.xlf.common.core.util.file.FileUtils;
import com.xlf.common.core.util.file.QinIuUtils;
import com.xlf.common.core.util.file.UploadConfig;
import com.xlf.service.system.domain.vo.QinIuVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 通用请求处理
 */
@RestController
@RequestMapping("/system/common")
public class CommonController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(CommonController.class);


    private static final String FILE_DELIMETER = ",";


    @Resource
    QinIuUtils qinIuUtils;

    /**
     * 本地通用上传请求（单个）
     */
    @PostMapping("/upload")
    @PreAuthorize("@ss.hasPermi('common:upload')")
    public AjaxResult uploadFile(MultipartFile file) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = UploadConfig.getUploadPath();

            // 上传并返回url给前端访问。给项目下的路径，让前端自己去拼接/api
            String path = FileUploadUtils.upload(filePath, file);

            AjaxResult ajax = AjaxResult.success();
            //前端访问路径
            ajax.put("url", FileUploadUtils.getFileUrl(path));
            //文件储存路径
            ajax.put("fileName", path);
            //最终在数据库的文件名
            ajax.put("newFileName", FileUtils.getName(path));
            //原来的文件名
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 通用上传请求（多个）
     */
    @Deprecated
    @PostMapping("/uploads")
    @PreAuthorize("@ss.hasPermi('common:uploads')")
    public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = UploadConfig.getUploadPath();
            List<String> urls = new ArrayList<String>();
            List<String> fileNames = new ArrayList<String>();
            List<String> newFileNames = new ArrayList<String>();
            List<String> originalFilenames = new ArrayList<String>();
            for (MultipartFile file : files)
            {
                // 上传并返回url
                String url = FileUploadUtils.upload(filePath, file);
                urls.add(url);
                fileNames.add(url);
                newFileNames.add(FileUtils.getName(url));
                originalFilenames.add(file.getOriginalFilename());
            }
            AjaxResult ajax = AjaxResult.success();
            ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
            ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER));
            ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER));
            ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER));
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取七牛云上传token
     * @return
     */
    @PreAuthorize("@ss.hasPermi('common:upload')")
    @GetMapping("/getUploadToken")
    public AjaxResult getUploadToken(){
        return success(qinIuUtils.buildUpload());
    }
}
